﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using GlobalOfficeTools.Framework.CacheService;
using GlobalOfficeTools.Interfaces.Model;
using GlobalOfficeTools.MVC3.Models;
using GlobalOfficeTools.MVC3.DomainServices.Clients;
using System.ServiceModel.DomainServices.Client;

namespace GlobalOfficeTools.Models
{
    [Export(typeof(IClientsModel))]
    public class ClientsModel : IClientsModel
    {
        #region Private Fields 
        private ClientsDomainContext _context;
        public ICacheService _cacheService;
        #endregion

        #region Constructor 
        [ImportingConstructor]
        public ClientsModel([Import(typeof(ICacheService))] ICacheService cacheService) 
        {
            _context = new ClientsDomainContext();
            _cacheService = cacheService;
        }
        #endregion

        #region IClientsModel Implementation 
        public void AutheticateUser(string userName, string password, Action<CLItblUser, CLItblCompany, CLIvwEmployeeType> callBack) 
        {
            _context.Load(_context.GetUserQuery(userName, password, "LOCAL"), loadOpUser =>
            {
                if (loadOpUser.Entities.Count() == 0) callBack(null, null, null);

                int userId = loadOpUser.Entities.FirstOrDefault().UserId;
                int companyId = loadOpUser.Entities.FirstOrDefault().CLItblUserOption.DefaultCompanyId;

                _context.Load(_context.GetEmployeeTypeQuery(userId, companyId), loadEmployeeType =>
                {
                    _context.Load(_context.GetCompanyQuery(companyId), loadOpCompany =>
                    {
                        callBack(loadOpUser.Entities.FirstOrDefault(), loadOpCompany.Entities.FirstOrDefault(), loadEmployeeType.Entities.FirstOrDefault());
                    }, null);
                }, null);
            }, null);
        }
        public void GetAccessList(int userId, int companyId, Action<IEnumerable<CLItblAcl>> callBack) 
        {
            if (_cacheService.GetFromCache(_context.GetAccessListTableQuery(userId, companyId).ToString()) == null)
            {
                _context.Load(_context.GetAccessListTableQuery(userId, companyId), loadOp =>
                {
                    _cacheService.AddCache(_context.GetAccessListTableQuery(userId, companyId).ToString(), loadOp.Entities);
                    callBack(loadOp.Entities);
                }, null);
            }
            else
                callBack(_cacheService.GetFromCache(_context.GetAccessListTableQuery(userId, companyId).ToString()) as IEnumerable<CLItblAcl>);
        }
        //public void ListClientTypes(int companyId, Action<IEnumerable<CLIvwClientType>> callBack) 
        //{
        //    if (_cacheService.GetFromCache(_context.ListClientTypesQuery(companyId).ToString()) == null)
        //    {
        //        _context.Load(_context.ListClientTypesQuery(companyId), loadOp =>
        //        {
        //            _cacheService.AddCache(_context.ListClientTypesQuery(companyId).ToString(), loadOp.Entities);
        //            callBack(loadOp.Entities);
        //        }, null);
        //    }
        //    else
        //        callBack(_cacheService.GetFromCache(_context.ListClientTypesQuery(companyId).ToString()) as IEnumerable<CLIvwClientType>);
        //}
        //public void ListVendorTypes(int companyId, Action<IEnumerable<CLIvwVendorType>> callBack) 
        //{
        //    if (_cacheService.GetFromCache(_context.ListVendorTypesQuery(companyId).ToString()) == null)
        //    {
        //        _context.Load(_context.ListVendorTypesQuery(companyId), loadOp =>
        //        {
        //            _cacheService.AddCache(_context.ListClientTypesQuery(companyId).ToString(), loadOp.Entities);
        //            callBack(loadOp.Entities);
        //        }, null);
        //    }
        //    else
        //        callBack(_cacheService.GetFromCache(_context.ListVendorTypesQuery(companyId).ToString()) as IEnumerable<CLIvwVendorType>);
        //}
        //public void ListUserGroups(int companyId, Action<IEnumerable<CLIvwUserGroup>> callBack) 
        //{
        //    if (_cacheService.GetFromCache(_context.ListUserGroupsQuery(companyId).ToString()) == null)
        //    {
        //        _context.Load(_context.ListUserGroupsQuery(companyId), loadOp =>
        //        {
        //            _cacheService.AddCache(_context.ListClientTypesQuery(companyId).ToString(), loadOp.Entities);
        //            callBack(loadOp.Entities);
        //        }, null);
        //    }
        //    else
        //        callBack(_cacheService.GetFromCache(_context.ListUserGroupsQuery(companyId).ToString()) as IEnumerable<CLIvwUserGroup>);
        //}
        //public void ListEmployees(int companyId, Action<IEnumerable<CLIvwEmployee>> callBack) 
        //{
        //    if (_cacheService.GetFromCache(_context.ListEmployeesQuery(companyId).ToString()) == null)
        //    {
        //        _context.Load(_context.ListEmployeesQuery(companyId), loadOp =>
        //        {
        //            _cacheService.AddCache(_context.ListEmployeesQuery(companyId).ToString(), loadOp.Entities);
        //            callBack(loadOp.Entities);
        //        }, null);
        //    }
        //    else
        //        callBack(_cacheService.GetFromCache(_context.ListEmployeesQuery(companyId).ToString()) as IEnumerable<CLIvwEmployee>);
        //}
        #endregion
    }
}
